package com.yanqu.road.dao.impl.union.honors;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.honors.UnionHistoryHonorsRecordData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionHistoryHonorsRecordDataDaoImpl extends TempDataObjectDao<UnionHistoryHonorsRecordData> {

    public static String getUnionHistoryHonorsRecordDataSql = "select * from `t_c_union_honor_history`";
    public Map<String, List<UnionHistoryHonorsRecordData>> getUnionHistoryHonorsRecordData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, List<UnionHistoryHonorsRecordData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionHistoryHonorsRecordDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionHistoryHonorsRecordData data = parse(rs);
                if(!result.containsKey(data.getUnionUid())){
                    result.put(data.getUnionUid(), new ArrayList<>());
                }
                result.get(data.getUnionUid()).add(data);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUnionHistoryHonorsRecordDataSql = "insert into `t_c_union_honor_history`(unionUid,activityId,activityType,activityChildType,getTime,honors,serverList,rank,goodId,unionNames) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionHistoryHonorsRecordData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityChildType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHonors()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getServerList()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGoodId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionNames()));
        if (getDb().executeNoneQuery(insertUnionHistoryHonorsRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }
    //无法更新，没有唯一
    public static String updateUnionHistoryHonorsRecordDataSql = "update `t_c_union_honor_history` set getTime=?,honors=?,serverList=?,activityType=?,activityChildType=?,rank=?,unionNames=? where unionUid=? and activityId = ?";

    @Override
    protected boolean updateData(UnionHistoryHonorsRecordData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getGetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHonors()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getServerList()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityChildType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionNames()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateUnionHistoryHonorsRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionHistoryHonorsRecordData parseData(ResultSet rs) throws SQLException {
        UnionHistoryHonorsRecordData data = new UnionHistoryHonorsRecordData();
        data.setUnionUid(rs.getString("unionUid"));
        data.setActivityId(rs.getInt("activityId"));
        data.setActivityType(rs.getInt("activityType"));
        data.setActivityChildType(rs.getInt("activityChildType"));
        data.setGetTime(rs.getInt("getTime"));
        data.setHonors(rs.getInt("honors"));
        data.setServerList(rs.getString("serverList"));
        data.setRank(rs.getInt("rank"));
        data.setGoodId(rs.getInt("goodId"));
        String str = rs.getString("unionNames");
        if(!StringUtils.isNullOrEmpty(str)) {
            data.setUnionNames(str);
        }
        return data;
    }
}
